package net.sudot.chess.business.controller.web;

import net.sudot.chess.business.controller.AbstractBaseController;
import net.sudot.chess.business.model.RechargeConfig;
import net.sudot.chess.business.service.PluginService;
import net.sudot.chess.business.service.RechargeConfigService;
import net.sudot.chess.plugin.payment.PaymentPlugin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充值
 *
 * @author tangjialin on 2018-03-17.
 */
@Controller
@RequestMapping(value = "/recharge")
public class RechargeController extends AbstractBaseController {

    @Resource
    private RechargeConfigService rechargeConfigService;
    @Resource
    private PluginService pluginService;

    /**
     * 充值中心
     *
     * @return 返回充值中心视图
     */
    @GetMapping("/index")
    public String index() {
        return "recharge/index";
    }

    /**
     * 充值配置信息
     *
     * @return 返回充值配置
     */
    @PostMapping("/config")
    @ResponseBody
    public List<RechargeConfig> config() {
        return rechargeConfigService.findAll();
    }

    /**
     * 充值插件信息
     *
     * @return 返回充值插件
     */
    @PostMapping("/plugin")
    @ResponseBody
    public List<Map<String, String>> plugin(HttpServletRequest request) {
        List<PaymentPlugin> paymentPlugins = pluginService.getActivePaymentPlugins(request);
        List<Map<String, String>> mapArrayList = new ArrayList<>();
        HashMap<String, String> map = null;
        for (PaymentPlugin plugin : paymentPlugins) {
            map = new HashMap<>();
            map.put("label", plugin.getDisplayName());
            map.put("value", plugin.getId());
            mapArrayList.add(map);
        }
        return mapArrayList;
    }

}
